package main

import (
	"flag"
	"fmt"
	"net/http"
)

func handleHome(w http.ResponseWriter, r *http.Request) {
	if r.URL.Path != "/" {
		http.Error(w, "Not Found", http.StatusNotFound)
		return
	}
	if r.Method != "GET" {
		http.Error(w, "Method not allow", http.StatusMethodNotAllowed)
		return
	}

	http.ServeFile(w, r, "home.html")

}

func main() {
	port := flag.String("port", "5657", "http service port")
	flag.Parse()
	hub := NewHub()
	go hub.Run()

	http.HandleFunc("/", handleHome)

	http.HandleFunc("/ws", func(w http.ResponseWriter, r *http.Request) {
		ServerWs(hub, w, r)
	})
	if err := http.ListenAndServe(":"+*port, nil); err != nil {
		fmt.Printf("start http failed: %v\n", err)
	}
}
